package com.zlx.sharelive.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.AgreementBean;
import com.zlx.module_base.base_api.res_data.LoginCodeBean;
import com.zlx.module_base.base_api.res_data.RegisterBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.utils.StringUt;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册页面
 */
public class RegisterAc extends BaseAc {

    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.iv_register)
    ImageView iv_register;
    @BindView(R.id.et_pass1)
    EditText et_pass1;
    @BindView(R.id.et_pass2)
    EditText et_pass2;
    @BindView(R.id.et_invitation)
    EditText et_invitation;
    @BindView(R.id.tv_area_code)
    TextView tv_area_code;
    @BindView(R.id.tv_code)
    TextView tv_code;

    private TimeCount time;
    private String smsType = "1";//验证码类型 1,注册 2 绑定手机号
    private boolean invitation = false;//是否勾选

    @Override
    protected int getLayoutId() {
        return R.layout.ac_register;
    }

    @Override
    public void initViews() {
        super.initViews();

        time = new TimeCount(60000, 1000);
    }

    @OnClick({R.id.tv_code, R.id.tv_register, R.id.tv_agreement1, R.id.tv_agreement2
            , R.id.iv_register, R.id.ll_area_code})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.ll_area_code:
                //区号
                startActivityForResult(new Intent(this, AreaCodeListAc.class), 20);
                break;
            case R.id.tv_agreement1:
                //用户协议
                getAgreement("3");
                break;
            case R.id.tv_agreement2:
                //隐私协议
                getAgreement("4");
                break;
            //勾选
            case R.id.iv_register:
                if (invitation == true) {
                    invitation = false;
                    iv_register.setImageResource(R.mipmap.tick_false);
                } else {
                    invitation = true;
                    iv_register.setImageResource(R.mipmap.tick_true);
                }
                break;
            case R.id.tv_code:
                //获取验证码
                if (StringUt.isEmpty(et_phone.getText().toString().trim())) {
                    Toast.makeText(this, "手机号不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    loginCode();
                }
                break;
            //注册
            case R.id.tv_register:
                if (StringUt.isEmpty(et_phone.getText().toString().trim())) {
                    Toast.makeText(this, "手机号不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (StringUt.isEmpty(et_code.getText().toString().trim())) {
                    Toast.makeText(this, "验证码不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (et_pass1.getText().toString().trim().length() != 6) {
                    Toast.makeText(this, "请输入6位密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (et_pass2.getText().toString().trim().length() != 6) {
                    Toast.makeText(this, "请输入6位密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!et_pass1.getText().toString().trim().equals(et_pass2.getText().toString().trim())) {
                    Toast.makeText(this, getResources().getString(R.string.the_two_passwords_do_not_match), Toast.LENGTH_SHORT).show();
                    return;
                } else if (invitation == false) {
                    Toast.makeText(this, getResources().getString(R.string.please_agree_to_the_relevant_agreement), Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    loginRegister();
                }
                break;
        }
    }

    private void getAgreement(String type) {
        ApiUtil.getHomeApi().getAgreement(type).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<AgreementBean>>() {
            @Override
            public void onSuccess(ApiResponse<AgreementBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                startActivity(new Intent(RegisterAc.this, WebViewActivity.class)
                                        .putExtra("type", type)
                                        .putExtra("title", data.getData().getInfo().get(0).getPost_title())
                                        .putExtra("content", data.getData().getInfo().get(0).getPost_content())
                                );
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    /**
     * 注册
     */
    private void loginRegister() {
        ApiUtil.getLoginApi().login_register(
                (tv_area_code.getText().toString().trim() + et_phone.getText().toString().trim()).replace("+", ""),
                et_code.getText().toString().trim(),
                et_pass1.getText().toString().trim(),
                et_pass2.getText().toString().trim(),
                et_invitation.getText().toString().trim()).observe(
                this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<RegisterBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<RegisterBean> data) {
                        if (data.getData().getCode() == 0) {
                            Toast.makeText(RegisterAc.this,
                                    getResources().getString(R.string.registration_success),
                                    Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(RegisterAc.this,
                                    data.getData().getMsg(),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        super.onFail(msg);
                        Toast.makeText(RegisterAc.this, msg, Toast.LENGTH_SHORT).show();
                    }
                }));
    }

    /**
     * 获取验证码
     */
    private void loginCode() {
        ApiUtil.getLoginApi().login_code((tv_area_code.getText().toString().trim() + et_phone.getText().toString().trim()).replace("+", ""), smsType)
                .observe(this,
                        new BaseObserver<>(new BaseObserverCallBack<ApiResponse<LoginCodeBean>>() {
                            @Override
                            public void onSuccess(ApiResponse<LoginCodeBean> data) {
                                if (null != data) {
                                    if (null != data.getData()) {
                                        if (data.getData().getCode() == 1002) {
                                            time.start();
                                        } else {
                                            Toast.makeText(RegisterAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }
                            }

                            @Override
                            public void onFail(String msg) {
                                super.onFail(msg);

                            }
                        }));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null == data) {
            return;
        }
        if (resultCode == 30) {
            tv_area_code.setText(data.getStringExtra("code"));
        }
    }

    public class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_code.setTextColor(Color.parseColor("#666666"));
            tv_code.setClickable(false);
            tv_code.setText("(" + millisUntilFinished / 1000 + ") s");
        }

        @Override
        public void onFinish() {
            tv_code.setText(getResources().getText(R.string.resend_code));
            tv_code.setClickable(true);
            tv_code.setTextColor(Color.parseColor("#FF64B4"));
        }
    }
}
